/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.escom.healthlc.utils;

import com.escom.healthlc.entidad.EnfermedadVO;
import java.lang.reflect.Field;
import java.util.Comparator;
import org.primefaces.model.SortOrder;

/**
 *
 * @author LAURA
 */
public class LazySorterEnfermedad implements Comparator<EnfermedadVO> {
 
    private String sortField;

    private SortOrder sortOrder;

    public LazySorterEnfermedad(String sortField, SortOrder sortOrder) {
        this.sortField = sortField;
        this.sortOrder = sortOrder;
    }

    public int compare(EnfermedadVO enfermedad1, EnfermedadVO enfermedad2) {
       try {

            Object value1;
            Object value2;
            Field field1;
            Field field2;
            String compere = sortField;
//            System.out.println(sortField);

            field1 = enfermedad1.getClass().getDeclaredField(sortField);
            field2 = enfermedad2.getClass().getDeclaredField(sortField);
            field1.setAccessible(true);
            field2.setAccessible(true);
            value1 = field1.get(enfermedad1);
            value2 = field2.get(enfermedad2);

            int value = ((Comparable) value1).compareTo(value2);

            return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}
